<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/24
 * Time: 13:02
 */
$server = new swoole_websocket_server("0.0.0.0", 9501);
//server->set([]);可以设置参数
//$server->set(
//    [
//        'enable_static_handler' => true,
//        'document_root' => "/home/work/swoole_mooc/data",
//    ]
//);

//监听websocket连接打开事件
$server->on('open', 'onOpen');
function onOpen($server, $request)
{
    print_r($request->fd);
}

//监听ws消息时间
$server->on('message', function (swoole_websocket_server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    //把数据推送到客户端,参数：1.客户端id 2.数据
    $server->push($frame->fd, "推送的数据");
});

//关闭时间，关闭哪个客户端
$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});

$server->start();